package com.nageoffer.shortlink.admin.remote.dto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.dto.req.RecycleBinSaveReqDTO;
import com.nageoffer.shortlink.admin.remote.dto.req.*;
import com.nageoffer.shortlink.admin.remote.dto.resp.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 * 中转调用api
 */
public interface ShortLinkRemoteService {


    /**
     * 新建短链接
     * @param requestParam
     * @return
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDto requestParam){
        String resultBodyStr = HttpUtil.post("localhost:8002/api/short-link/v1/create",JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr, new TypeReference<>() {
        });

    }


    /**
     * 中转分页查询短链接
     * @param requsetParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requsetParam){
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid",requsetParam.getGid());
        requestMap.put("current",requsetParam.getCurrent());
        requestMap.put("size",requsetParam.getSize());
        String resultPageStr = HttpUtil.get("localhost:8002/api/short-link/v1/page",requestMap);
        // new TypeReference json反序列化泛型的隐式转化
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });


    }

    /**
     * 查询分组短链接总量
     * @param requestParam 短链接分组总量请求参数
     * @return 查询分组短链接总量响应
     */

    default Result<List<ShortLinkCountQueryReqDTO>> listGroupShortLinkCount(List<String> requestParam){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resultCount = HttpUtil.get("localhost:8002/api/short-link/v1/count",requestMap);
        return JSON.parseObject(resultCount, new TypeReference<Result<List<ShortLinkCountQueryReqDTO>>>() {
        });
    }

    /**
     * 修改短链接
     * @param requestParam
     */
     default void updateShortLink(ShortLinkUpdateReqDto requestParam){
         HttpUtil.post("localhost:8002/api/short-link/v1/update",JSON.toJSONString(requestParam));
     };



    /**
     *
     * @param url 目标网站地址
     * @return 网站标题
     */
     default Result<String> getTitleByUrl(@RequestParam("url") String url){
         String resultStr = HttpUtil.get("localhost:8002/api/short-link/v1/title?url=" + url);
         return JSON.parseObject(resultStr, new TypeReference<>() {
         });
     }

    /**
     * 保存回收站
     * @param requestParam
     * @return
     */
    default void saveRecycleBin(RecycleBinSaveReqDTO requestParam){
        HttpUtil.post("localhost:8002/api/short-link/v1/recycle-bin/save",JSON.toJSONString(requestParam));
    }

    /**
     * 分页查询回收站短链接
     * @param requsetParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requsetParam){
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requsetParam.getGidList());
        requestMap.put("current",requsetParam.getCurrent());
        requestMap.put("size",requsetParam.getSize());
        String resultPageStr = HttpUtil.get("localhost:8002/api/short-link/v1/recycle-bin/page",requestMap);
        // new TypeReference json反序列化泛型的隐式转化
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });

    }

    /**
     * 回收站恢复
     * @param requestParam
     */
    default void recycleBinRecover(RecycleBinRecoverReqDTO  requestParam){
        HttpUtil.post("localhost:8002/api/short-link/v1/recycle-bin/recover",JSON.toJSONString(requestParam));
    }


    /**
     * 回收站删除
     * @param requestParam
     */
    default void recycleBinRemove(RecycleBinRemoveReqDTO  requestParam){
        HttpUtil.post("localhost:8002/api/short-link/v1/recycle-bin/remove",JSON.toJSONString(requestParam));
    }

    /**
     * 监控单个短链接
     * @param requestParam
     * @return
     */
    default Result<ShortLinkStatsRespDTO> oneShortLinkStats(ShortLinkStatsReqDTO requestParam){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("fullShortUrl",requestParam.getFullShortUrl());
        requestMap.put("startDate",requestParam.getStartDate());
        requestMap.put("endDate",requestParam.getEndDate());
        String resultStr = HttpUtil.get("localhost:8002/api/short-link/v1/stats", requestMap);
        return JSON.parseObject(resultStr, new TypeReference<>() {
        });
    }

    /**
     * 分页查询短链接日志
     */

    default Result<IPage<ShortLinkAccessRecordRespDto>> shortLinkStatsAccessRecord(ShortLinkAccessRecordReqDTO requestParam){
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(requestParam);
        stringObjectMap.remove("orders");
        stringObjectMap.remove("records");
        String s = HttpUtil.get("localhost:8002/api/short-link/v1/stats/access-record", stringObjectMap);
        return JSON.parseObject(s, new TypeReference<>() {
        });
    }





}
